import { Engine } from "../../Engine";
import { Object3D } from "../../entity/index";
import { CylinderGeometry, RectangleGeometry } from "../../geom/index";
import { Register } from "../../register/Register";
import { Group } from "../../scene/Group";
import { Component, ComponentType } from "../Component";
import { Mesh } from "../Mesh";
import { BasicMaterial } from "../../material/index";
import { EventType } from "../../event/index";
import { Vector3 } from "../../core/math/Vector3";
class Transform extends Component {
    static that;
    constructor(opts = {}) {
        opts.type = ComponentType.Transform;
        super(opts);
        this._enable = false;
        this.behavior = "";
        this.helpSize = 200;//100个像素
        this.helperObject = null;
        this._mode = 1;
        Transform.that = this;
    }
    set mode(v) {
        this._mode = v;
        if(!this.target){
            return;
        }
        let scale = (v & (1 << 2 | 0)) !== 0;
        let rotation = (v & (1 << 1 | 0)) !== 0;
        let trans = (v & (1 << 0 | 0)) !== 0
        Transform.that.helperObject.get("__txa").setVisibility(trans);
        Transform.that.helperObject.get("__tx").setVisibility(trans);
        Transform.that.helperObject.get("__tya").setVisibility(trans);
        Transform.that.helperObject.get("__ty").setVisibility(trans);
        Transform.that.helperObject.get("__tza").setVisibility(trans);
        Transform.that.helperObject.get("__tz").setVisibility(trans);
        Transform.that.helperObject.get("__txy").setVisibility(trans);
        Transform.that.helperObject.get("__txz").setVisibility(trans);
        Transform.that.helperObject.get("__tyz").setVisibility(trans);

        Transform.that.helperObject.get("__rx").setVisibility(rotation);
        Transform.that.helperObject.get("__ry").setVisibility(rotation);
        Transform.that.helperObject.get("__rz").setVisibility(rotation);

        Transform.that.helperObject.get("__sxa").setVisibility(scale);
        Transform.that.helperObject.get("__sx").setVisibility(scale);
        Transform.that.helperObject.get("__sya").setVisibility(scale);
        Transform.that.helperObject.get("__sy").setVisibility(scale);
        Transform.that.helperObject.get("__sza").setVisibility(scale);
        Transform.that.helperObject.get("__sz").setVisibility(scale);
        Transform.that.helperObject.get("__sxy").setVisibility(scale);
        Transform.that.helperObject.get("__sxz").setVisibility(scale);
        Transform.that.helperObject.get("__syz").setVisibility(scale);
    }
    get mode() {
        return this._mode;
    }
    get pickControl() {
        if (!this._pickControl) {
            this._pickControl = Register.instance.manager(ComponentType.Pick).get("x");
            // debugger
            // this._pickControl.action=["click","mousemove"];
        }
        this._pickControl.action=["click","mousemove"];
        return this._pickControl;
    }
    run(event) {
        if (event.componentType == ComponentType.Camera) {
            if (Transform.that.helperObject) {
                let scale = event.target.distance / event.target.size[1] * Transform.that.helpSize;
                Transform.that.helperObject.trans.scale = [scale, scale, scale];
            }
        }
    }
    transformEnd(event) {
        if (Transform.that.behavior != "") {
            //Transform.that.off("mousemove", Transform.that.transform);
            Transform.that.reset();
            Transform.that.behavior="";
        }
        
    }
    transformBegin(event) {
        if (event.originEvent.button == 0) {
            Transform.that.pre = Transform.that.cur = event.data.pixel;
           // Transform.that.on("mousemove", Transform.that.transform);
        }

    }
    transform(event) {
        if (event.originEvent.buttons == 1) {
            let cpos, ppos, z, scale,q;
            let sx,sy,sz;
            const camera = Engine.instance.scene.getCamera();
            switch (Transform.that.behavior) {
                case "tx":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    for(let i=0;i<this.trans.length;i++){
                        Transform.that.trans[i].translateX(cpos.x);
                    }
                    Transform.that.helperObject.trans.position = [...Transform.that.trans[0].position.toArray()];
                    break;
                case "ty":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    for(let i=0;i<this.trans.length;i++){
                        Transform.that.trans[i].translateY(cpos.y);
                    }

                    Transform.that.helperObject.trans.position = [...Transform.that.trans[0].position.toArray()];
                    break;
                case "tz":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    for(let i=0;i<this.trans.length;i++){
                        Transform.that.trans[i].translateZ(cpos.z);
                    }
                    Transform.that.helperObject.trans.position = [...Transform.that.trans[0].position.toArray()];
                    break;
                case "txy":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    for(let i=0;i<this.trans.length;i++){
                        Transform.that.trans[i].translateX(cpos.x);
                        Transform.that.trans[i].translateY(cpos.y);
                    }
                    Transform.that.helperObject.trans.position = [...Transform.that.trans[0].position.toArray()];
                    break;
                case "txz":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    for(let i=0;i<this.trans.length;i++){
                        Transform.that.trans[i].translateX(cpos.x);
                        Transform.that.trans[i].translateZ(cpos.z);
                    }
                    Transform.that.helperObject.trans.position = [...Transform.that.trans[0].position.toArray()];
                    break;
                case "tyz":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    for(let i=0;i<this.trans.length;i++){
                        Transform.that.trans[i].translateY(cpos.y);
                        Transform.that.trans[i].translateZ(cpos.z);
                    }
                    Transform.that.helperObject.trans.position = [...Transform.that.trans[0].position.toArray()];
                    break;
                //scale   
                case "sx":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    sx = 1 + cpos.x / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    for(let i=0;i<this.trans.length;i++){
                        scale=Transform.that.trans[i].scale;
                        Transform.that.trans[i].scale = [scale.x * sx, scale.y, scale.z];
                    }
                    break;
                case "sy":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    sy = 1 + cpos.y / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    for(let i=0;i<this.trans.length;i++){
                        scale=Transform.that.trans[i].scale;
                        Transform.that.trans[i].scale = [scale.x, scale.y * sy, scale.z];
                    }
                    break;
                case "sz":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    sz = 1 + cpos.z / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    for(let i=0;i<this.trans.length;i++){
                        scale=Transform.that.trans[i].scale;
                        Transform.that.trans[i].scale = [scale.x, scale.y, scale.z * sz];
                    }
                    break;
                case "sxy":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    sx= 1 + cpos.x / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    sy= 1 + cpos.y / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    for(let i=0;i<this.trans.length;i++){
                        scale=Transform.that.trans[i].scale;
                        Transform.that.trans[i].scale = [scale.x*sx, scale.y*sy, scale.z];
                    }
                    break;
                case "sxz":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    sx= 1 + cpos.x / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    sz= 1 + cpos.z / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    for(let i=0;i<this.trans.length;i++){
                        scale=Transform.that.trans[i].scale;
                        Transform.that.trans[i].scale = [scale.x*sx, scale.y, scale.z*sz];
                    }
                    break;
                case "syz":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    sy= 1 + cpos.y / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    sz= 1 + cpos.z / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    for(let i=0;i<this.trans.length;i++){
                        scale=Transform.that.trans[i].scale;
                        Transform.that.trans[i].scale = [scale.x, scale.y*sy, scale.z*sz];
                    }
                    break;
                //rotation
                case "rx":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    scale = (cpos.length() * Math.sign(Transform.that.cur[1] - Transform.that.pre[1])) / (camera.distance / camera.size[1] * Transform.that.helpSize);
                    //let orx=Transform.that.target.trans.rotation;
                    for(let i=0;i<this.trans.length;i++){
                        Transform.that.trans[i].rotateX(scale);
                    }
                    Transform.that.helperObject.trans.rotation = [...Transform.that.trans[0].rotation.toArray()];
                    break;
                case "ry":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[0] - Transform.that.cur[0]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    scale = (cpos.length() * Math.sign(Transform.that.cur[1] - Transform.that.pre[1])) / (camera.distance / camera.size[1] * Transform.that.helpSize);

                    //let ory=Transform.that.target.trans.rotation;
                    for(let i=0;i<this.trans.length;i++){
                        Transform.that.trans[i].rotateY(scale);
                    }
                    Transform.that.helperObject.trans.rotation = [...Transform.that.trans[0].rotation.toArray()];
                    break;
                case "rz":
                    event.preventDefault();
                    Transform.that.pre = Transform.that.cur;
                    Transform.that.cur = event.data.pixel;
                    z = (Transform.that.pre[1] - Transform.that.cur[1]) * (camera.distance / camera.size[1]);
                    cpos = camera.pixelToWorld([Transform.that.cur[0], Transform.that.cur[1] - Transform.that.pre[1] + camera.size[1] / 2, z]);
                    ppos = camera.pixelToWorld([Transform.that.pre[0], camera.size[1] / 2]);
                    cpos.sub(ppos);
                    q=Transform.that.target.trans.quaternion.clone().invert();
                    cpos.applyQuaternion(q);
                    scale = (cpos.length() * Math.sign(Transform.that.cur[1] - Transform.that.pre[1])) / (camera.distance / camera.size[1] * Transform.that.helpSize);

                    //let orz=Transform.that.target.trans.rotation;
                    for(let i=0;i<this.trans.length;i++){
                        Transform.that.trans[i].rotateZ(scale);
                    }
                    Transform.that.helperObject.trans.rotation = [...Transform.that.trans[0].rotation.toArray()];
                    break;
            }
        }

    }
    active(){
        if(!this.enable){
            super.active();
            this.register();
        }
    }
    deactive() {
        super.deactive();
        this.mode = 0;
        this.unregister();
    }
    register() {
        this.pickControl.select(this.select, "mousemove");
        this.on(EventType.ComponentEvent, this.run);
        //注册键盘事件
        window.addEventListener("keydown", this.keydown);
        this.on("mousedown", this.transformBegin);
        this.on("mousemove", this.transform);
        this.on("mouseup", this.transformEnd);
    }
    unregister() {
        this.pickControl.offSelect(this.select, "mousemove");
        this.off(EventType.ComponentEvent, this.run);
        window.removeEventListener("keydown", this.keydown);
        this.off("mousedown", this.transformBegin);
        this.off("mousemove", this.transform);
        this.off("mouseup", this.transformEnd);
    }
    unBind(){
        if(Transform.that.helperObject){
            Transform.that.mode = 0;
            Transform.that.unregister();
        }
        
    }
    bind(trans,target) {
        if (!target || !this.enable ||target.getRoot().id == "__transform__") {
            return;
        }
        if(!trans||trans.length<=0){
            return;
        }
        this.target=target;
        this.trans = trans;
        //添加辅助实体
        if (!this.helperObject) {
            this.createHelperObject(this);
        } else {
            this.helperObject.trans.position = [...this.trans[0].position.toArray()];
            this.helperObject.trans.rotation = [...this.trans[0].rotation.toArray()];
        }
        if (this.mode == 0) {
            this.mode = 1;
        } else {
            this.mode = this.mode;//跟新模式
        }
        this.unregister();
        this.register();

    }
    keydown(e) {
        const camera = Engine.instance.scene.getCamera();
        let scale = camera.distance / camera.size[1] * Transform.that.helpSize;
        switch (e.key.toLowerCase()) {
            case "=":
                Transform.that.helpSize *= 1.1;
                scale = camera.distance / camera.size[1] * Transform.that.helpSize;
                Transform.that.helperObject.trans.scale = [scale, scale, scale];
                break;
            case "-":
                Transform.that.helpSize /= 1.1;
                scale = camera.distance / camera.size[1] * Transform.that.helpSize;
                Transform.that.helperObject.trans.scale = [scale, scale, scale];
                break;
            case "w":
                Transform.that.reset();
                Transform.that.mode = 1;
                break;
            case "e":
                Transform.that.reset();
                Transform.that.mode = 2;
                break;
            case "r":
                Transform.that.reset();
                Transform.that.mode = 4;
                break;
            case "t":
                Transform.that.reset();
                Transform.that.mode = 7;
                break;
            case "escape":
                Transform.that.mode = 0;
                Transform.that.deactive();
                break;
        }
    }
    reset() {
        if (!Transform.that.target) {
            return;
        }
        Transform.that.helperObject.get("__txa").mesh.material.color = "#ff0000";
        Transform.that.helperObject.get("__tx").mesh.material.color = "#ff0000";
        Transform.that.helperObject.get("__tya").mesh.material.color = "#00ff00";
        Transform.that.helperObject.get("__ty").mesh.material.color = "#00ff00";
        Transform.that.helperObject.get("__tza").mesh.material.color = "#0000ff";
        Transform.that.helperObject.get("__tz").mesh.material.color = "#0000ff";
        Transform.that.helperObject.get("__txy").mesh.material.color = "#0000ff";
        Transform.that.helperObject.get("__txz").mesh.material.color = "#00ff00";
        Transform.that.helperObject.get("__tyz").mesh.material.color = "#ff0000";

        Transform.that.helperObject.get("__rx").mesh.material.color = "#ff0000";
        Transform.that.helperObject.get("__ry").mesh.material.color = "#00ff00";
        Transform.that.helperObject.get("__rz").mesh.material.color = "#0000ff";

        Transform.that.helperObject.get("__sxa").mesh.material.color = "#ff0000";
        Transform.that.helperObject.get("__sx").mesh.material.color = "#ff0000";
        Transform.that.helperObject.get("__sya").mesh.material.color = "#00ff00";
        Transform.that.helperObject.get("__sy").mesh.material.color = "#00ff00";
        Transform.that.helperObject.get("__sza").mesh.material.color = "#0000ff";
        Transform.that.helperObject.get("__sz").mesh.material.color = "#0000ff";
        Transform.that.helperObject.get("__sxy").mesh.material.color = "#0000ff";
        Transform.that.helperObject.get("__sxz").mesh.material.color = "#00ff00";
        Transform.that.helperObject.get("__syz").mesh.material.color = "#ff0000";
        Transform.that.behavior = "";
    }
    select(result) {
        if (result.entity) {
            switch (result.entity.id) {
                case "__txa":
                case "__tx":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__txa").mesh.material.color = "#ffff00";
                    Transform.that.helperObject.get("__tx").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "tx";
                    break;
                case "__tya":
                case "__ty":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__tya").mesh.material.color = "#ffff00";
                    Transform.that.helperObject.get("__ty").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "ty";
                    break;
                case "__tza":
                case "__tz":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__tza").mesh.material.color = "#ffff00";
                    Transform.that.helperObject.get("__tz").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "tz";
                    break;
                case "__txy":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__txy").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "txy";
                    break;
                case "__txz":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__txz").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "txz";
                    break;
                case "__tyz":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__tyz").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "tyz";
                    break;
                case "__rx":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__rx").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "rx";
                    break;
                case "__ry":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__ry").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "ry";
                    break;
                case "__rz":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__rz").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "rz";
                    break;

                case "__sxa":
                case "__sx":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__sxa").mesh.material.color = "#ffff00";
                    Transform.that.helperObject.get("__sx").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "sx";
                    break;
                case "__sya":
                case "__sy":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__sya").mesh.material.color = "#ffff00";
                    Transform.that.helperObject.get("__sy").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "sy";
                    break;
                case "__sza":
                case "__sz":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__sza").mesh.material.color = "#ffff00";
                    Transform.that.helperObject.get("__sz").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "sz";
                    break;
                case "__sxy":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__sxy").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "sxy";
                    break;
                case "__sxz":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__sxz").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "sxz";
                    break;
                case "__syz":
                    Transform.that.reset();
                    Transform.that.helperObject.get("__syz").mesh.material.color = "#ffff00";
                    Transform.that.behavior = "syz";
                    break;
            }
        }
    }
    createHelperObject() {//创建辅助实体
        //创建锚点
        const camera = Engine.instance.scene.getCamera();
        let s = camera.distance / camera.size[1] * this.helpSize;
        this.helperObject = new Object3D({ id: "__transform__", position: this.target.trans.position.toArray(), scale: [s, s, s], rotation: this.target.trans.rotation.toArray() });
        let group = Register.instance.entityManager.get("__transform");
        if (!group) {
            group = new Group({ id: "__transform", name: "transform" });
            Engine.instance.scene.add(group);
        }

        //创建平移实体
        //x轴
        let arrawGeometry = new CylinderGeometry({
            radiusTop: 0, //圆柱顶部半径
            radiusBottom: 0.05,//圆柱底部半径
            height: 0.2,//圆柱高度
            heightSegments: 1,//高度方向段数
            radialSegments: 16//半径段数
        })
        let axisGeometry = new CylinderGeometry({
            radiusTop: 0.01, //圆柱顶部半径
            radiusBottom: 0.01,//圆柱底部半径
            height: 0.8,//圆柱高度
            heightSegments: 1,//高度方向段数
            radialSegments: 16//半径段数
        });
        let areaGeometry = new RectangleGeometry({ width: 0.2, height: 0.2 })
        let [position, indices] = this.torus(50, 50, 0.01, 1);
        let circleGeometry = new w.geometry.Geometry();
        circleGeometry.position = new Float32Array(position);
        circleGeometry.indices = new Uint16Array(indices);
        let axisXMaterial = new BasicMaterial({ color: "#ff0000", alphaCutoff: 0.0, defuseLight: true, depthWrite: false, depthFunc: "always",receiveShadow:false });
        let axisYMaterial = new BasicMaterial({ color: "#00ff00", alphaCutoff: 0.0, defuseLight: true, depthWrite: false, depthFunc: "always",receiveShadow:false });
        let axisZMaterial = new BasicMaterial({ color: "#0000ff", alphaCutoff: 0.0, defuseLight: true, depthWrite: false, depthFunc: "always",receiveShadow:false });

        let areaXMaterial = new BasicMaterial({ color: "#ff0000", alphaCutoff: 0.0, defuseLight: true, depthWrite: false, depthFunc: "always", side: "both",receiveShadow:false });
        let areaYMaterial = new BasicMaterial({ color: "#00ff00", alphaCutoff: 0.0, defuseLight: true, depthWrite: false, depthFunc: "always", side: "both" ,receiveShadow:false});
        let areaZMaterial = new BasicMaterial({ color: "#0000ff", alphaCutoff: 0.0, defuseLight: true, depthWrite: false, depthFunc: "always", side: "both" ,receiveShadow:false});

        let rotationXMaterial = new BasicMaterial({ color: "#ff0000", alphaCutoff: 0.0, defuseLight: true, depthWrite: false, depthFunc: "always", cameraSide: true,receiveShadow:false });
        let rotationYMaterial = new BasicMaterial({ color: "#00ff00", alphaCutoff: 0.0, defuseLight: true, depthWrite: false, depthFunc: "always", cameraSide: true,receiveShadow:false });
        let rotationZMaterial = new BasicMaterial({ color: "#0000ff", alphaCutoff: 0.0, defuseLight: true, depthWrite: false, depthFunc: "always", cameraSide: true,receiveShadow:false });

        //rotation
        //x
        let rotationXMesh = new Mesh({ geometry: circleGeometry, material: rotationXMaterial });
        let rotationX = new Object3D({ id: "__rx", rotation: [0, Math.PI / 2, 0], position: [0.0, 0.0, 0.0], scale: [1, 1, 1] });
        rotationX.addComponent(rotationXMesh);
        this.helperObject.add(rotationX);
        //Y
        let rotationYMesh = new Mesh({ geometry: circleGeometry, material: rotationYMaterial });
        let rotationY = new Object3D({ id: "__ry", rotation: [Math.PI / 2, 0, 0], position: [0.0, 0.0, 0.0], scale: [1, 1, 1] });
        rotationY.addComponent(rotationYMesh);
        this.helperObject.add(rotationY);
        //Z
        let rotationZMesh = new Mesh({ geometry: circleGeometry, material: rotationZMaterial });
        let rotationZ = new Object3D({ id: "__rz", rotation: [0, 0, 0], position: [0.0, 0.0, 0.0], scale: [1, 1, 1] });
        rotationZ.addComponent(rotationZMesh);
        this.helperObject.add(rotationZ);
        //scale
        //X轴
        let scaleXAMesh = new Mesh({ geometry: axisGeometry, material: axisXMaterial });
        let scaleXMesh = new Mesh({ geometry: axisGeometry, material: axisXMaterial });
        let scaleXA = new Object3D({ id: "__sxa", rotation: [0, 0, -Math.PI / 2], position: [0.9, 0.0, 0.0], scale: [5, 0.25, 5] });
        scaleXA.addComponent(scaleXAMesh);
        this.helperObject.add(scaleXA);
        let scaleX = new Object3D({ id: "__sx", rotation: [0, 0, -Math.PI / 2], position: [0.4, 0.0, 0.0] });
        scaleX.addComponent(scaleXMesh);
        this.helperObject.add(scaleX);
        //Y轴
        let scaleYAMesh = new Mesh({ geometry: axisGeometry, material: axisYMaterial });
        let scaleYMesh = new Mesh({ geometry: axisGeometry, material: axisYMaterial });
        let scaleYA = new Object3D({ id: "__sya", rotation: [0, 0, 0], position: [0.0, 0.9, 0.0], scale: [5, 0.25, 5] });
        scaleYA.addComponent(scaleYAMesh);
        this.helperObject.add(scaleYA);
        let scaleY = new Object3D({ id: "__sy", rotation: [0, 0, 0], position: [0.0, 0.4, 0.0] });
        scaleY.addComponent(scaleYMesh);
        this.helperObject.add(scaleY);
        //Z轴
        let scaleZAMesh = new Mesh({ geometry: axisGeometry, material: axisZMaterial });
        let scaleZMesh = new Mesh({ geometry: axisGeometry, material: axisZMaterial });
        let scaleZA = new Object3D({ id: "__sza", rotation: [Math.PI / 2, 0, 0], position: [0.0, 0.0, 0.9], scale: [5, 0.25, 5] });
        scaleZA.addComponent(scaleZAMesh);
        this.helperObject.add(scaleZA);
        let scaleZ = new Object3D({ id: "__sz", rotation: [Math.PI / 2, 0, 0], position: [0.0, 0.0, 0.4] });
        scaleZ.addComponent(scaleZMesh);
        this.helperObject.add(scaleZ);

        //x-y面
        let scaleXYMesh = new Mesh({ geometry: areaGeometry, material: areaZMaterial });
        let scaleXY = new Object3D({ id: "__sxy", rotation: [0, 0, 0], position: [0.3, 0.3, 0.0] });
        scaleXY.addComponent(scaleXYMesh);
        this.helperObject.add(scaleXY);

        //y-z面
        let scaleYZMesh = new Mesh({ geometry: areaGeometry, material: areaXMaterial });
        let scaleYZ = new Object3D({ id: "__syz", rotation: [0, -Math.PI / 2, 0], position: [0.0, 0.3, 0.3] });
        scaleYZ.addComponent(scaleYZMesh);
        this.helperObject.add(scaleYZ);

        //x-z面
        let scaleXZMesh = new Mesh({ geometry: areaGeometry, material: areaYMaterial });
        let scaleXZ = new Object3D({ id: "__sxz", rotation: [-Math.PI / 2, 0, 0], position: [0.3, 0.0, 0.3] });
        scaleXZ.addComponent(scaleXZMesh);
        this.helperObject.add(scaleXZ);

        //trans
        //X轴
        let transXAMesh = new Mesh({ geometry: arrawGeometry, material: axisXMaterial });
        let transXMesh = new Mesh({ geometry: axisGeometry, material: axisXMaterial });
        let transXA = new Object3D({ id: "__txa", rotation: [0, 0, -Math.PI / 2], position: [0.9, 0.0, 0.0] });
        transXA.addComponent(transXAMesh);
        this.helperObject.add(transXA);
        let transX = new Object3D({ id: "__tx", rotation: [0, 0, -Math.PI / 2], position: [0.4, 0.0, 0.0] });
        transX.addComponent(transXMesh);
        this.helperObject.add(transX);
        //Y轴    
        let transYAMesh = new Mesh({ geometry: arrawGeometry, material: axisYMaterial });
        let transYMesh = new Mesh({ geometry: axisGeometry, material: axisYMaterial });
        let transYA = new Object3D({ id: "__tya", rotation: [0, 0, 0], position: [0.0, 0.9, 0.0] });
        transYA.addComponent(transYAMesh);
        this.helperObject.add(transYA);
        let transY = new Object3D({ id: "__ty", rotation: [0, 0, 0], position: [0.0, 0.4, 0.0] });
        transY.addComponent(transYMesh);
        this.helperObject.add(transY);
        //Z轴
        let transZAMesh = new Mesh({ geometry: arrawGeometry, material: axisZMaterial });
        let transZMesh = new Mesh({ geometry: axisGeometry, material: axisZMaterial });
        let transZA = new Object3D({ id: "__tza", rotation: [Math.PI / 2, 0, 0], position: [0.0, 0.0, 0.9] });
        transZA.addComponent(transZAMesh);
        this.helperObject.add(transZA);
        let transZ = new Object3D({ id: "__tz", rotation: [Math.PI / 2, 0, 0], position: [0.0, 0.0, 0.4] });
        transZ.addComponent(transZMesh);
        this.helperObject.add(transZ);

        //x-y面
        let transXYMesh = new Mesh({ geometry: areaGeometry, material: areaZMaterial });
        let transXY = new Object3D({ id: "__txy", rotation: [0, 0, 0], position: [0.3, 0.3, 0.0] });
        transXY.addComponent(transXYMesh);
        this.helperObject.add(transXY);

        //y-z面
        let transYZMesh = new Mesh({ geometry: areaGeometry, material: areaXMaterial });
        let transYZ = new Object3D({ id: "__tyz", rotation: [0, -Math.PI / 2, 0], position: [0.0, 0.3, 0.3] });
        transYZ.addComponent(transYZMesh);
        this.helperObject.add(transYZ);

        //x-z面
        let transXZMesh = new Mesh({ geometry: areaGeometry, material: areaYMaterial });
        let transXZ = new Object3D({ id: "__txz", rotation: [-Math.PI / 2, 0, 0], position: [0.3, 0.0, 0.3] });
        transXZ.addComponent(transXZMesh);
        this.helperObject.add(transXZ);

        group.addEntity(this.helperObject);
    }
    pixelToWorld(pixel) {
        const camera = Engine.instance.scene.getCamera();
        let s = camera.distance / camera.size[1]
        return new Vector3(pixel[0] * s, pixel[1] * s, pixel[1] * s);
    }
    torus(row, column, irad, orad) {
        var position = new Array(),
            index = new Array();
        for (var i = 0; i <= row; i++) {
            var r = Math.PI * 2 / row * i; //管道圆上每个分段的弧度
            var rr = Math.cos(r);
            var ry = Math.sin(r);
            for (var ii = 0; ii <= column; ii++) {
                var tr = Math.PI * 2 / column * ii;
                //每个顶点位置的x、y、z分量
                var ty = (rr * irad + orad) * Math.cos(tr);
                var tz = ry * irad;
                var tx = (rr * irad + orad) * Math.sin(tr);

                var rx = rr * Math.cos(tr);
                var rz = rr * Math.sin(tr);
                position.push(tx, ty, tz);
            }
        }
        for (i = 0; i < row; i++) {
            for (ii = 0; ii < column; ii++) {
                r = (column + 1) * i + ii;
                index.push(r, r + column + 1, r + 1);
                index.push(r + column + 1, r + column + 2, r + 1);
            }
        }
        return [position, index];
    }

}
export { Transform }